package com.zhbit.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.zhbit.domain.StudentScore;


public class StudentScoreDaoImpl implements StudentScoreDao{
	@Resource
//	private Session session = null;
	private SessionFactory sessionFactory ;
//	private Transaction ts = null;
//	private String sql="";
	 
	public List findByPage(int pageNo){
		try{
			
			String queryString = "from StudentScore ";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult((pageNo-1)*5);
			
			queryObject.setMaxResults(5);
			return queryObject.list();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	public boolean add(int id,String username,String subject,float score) {
		try{
		//getSession();
		StudentScore stuscore = new StudentScore();
		stuscore.setId(id);
		stuscore.setUsername(username);
		stuscore.setSubject(subject);
		stuscore.setScore(score);
		getSession().save(stuscore);
		//ts.commit();
		return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(StudentScore stuscore){
		try{
			//getSession();
			getSession().update(stuscore);
			//ts.commit();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delstu(StudentScore stuscore){
		try{
			//getSession();
			getSession().delete(stuscore);
			//ts.commit();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public Session getSession() {
//        session = Hibernate.getSessionFactory().openSession();
        //ts = session.beginTransaction();
        return this.sessionFactory.getCurrentSession();
    }
	
}
